package org.dromara.system.service.claim.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.claim.ClaimAttach;
import org.dromara.system.domain.claim.bo.ClaimAttachBo;
import org.dromara.system.domain.claim.vo.ClaimAttachVo;
import org.dromara.system.mapper.claim.ClaimAttachMapper;
import org.dromara.system.service.claim.IClaimAttachService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 理赔附属Service业务层处理
 *
 * @author Lion Li
 * @date 2025-11-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ClaimAttachServiceImpl implements IClaimAttachService {

    private final ClaimAttachMapper baseMapper;

    /**
     * 查询理赔附属
     *
     * @param id 主键
     * @return 理赔附属
     */
    @Override
    public ClaimAttachVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询理赔附属列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 理赔附属分页列表
     */
    @Override
    public TableDataInfo<ClaimAttachVo> queryPageList(ClaimAttachBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClaimAttach> lqw = buildQueryWrapper(bo);
        Page<ClaimAttachVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的理赔附属列表
     *
     * @param bo 查询条件
     * @return 理赔附属列表
     */
    @Override
    public List<ClaimAttachVo> queryList(ClaimAttachBo bo) {
        LambdaQueryWrapper<ClaimAttach> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClaimAttach> buildQueryWrapper(ClaimAttachBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClaimAttach> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ClaimAttach::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getClaimId()), ClaimAttach::getClaimId, bo.getClaimId());
        lqw.eq(StringUtils.isNotBlank(bo.getRefusalReason()), ClaimAttach::getRefusalReason, bo.getRefusalReason());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditRemark()), ClaimAttach::getAuditRemark, bo.getAuditRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getUserFeedback()), ClaimAttach::getUserFeedback, bo.getUserFeedback());
        lqw.eq(StringUtils.isNotBlank(bo.getModifiedClaimMethod()), ClaimAttach::getModifiedClaimMethod, bo.getModifiedClaimMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getModifiedPaymentMethod()), ClaimAttach::getModifiedPaymentMethod, bo.getModifiedPaymentMethod());
        lqw.eq(bo.getAuditFee() != null, ClaimAttach::getAuditFee, bo.getAuditFee());
        lqw.eq(bo.getModifiedClaimedAmount() != null, ClaimAttach::getModifiedClaimedAmount, bo.getModifiedClaimedAmount());
        lqw.eq(bo.getNewMachinePrice() != null, ClaimAttach::getNewMachinePrice, bo.getNewMachinePrice());
        lqw.eq(bo.getUserPaidAmount() != null, ClaimAttach::getUserPaidAmount, bo.getUserPaidAmount());
        lqw.eq(bo.getMaterialFee() != null, ClaimAttach::getMaterialFee, bo.getMaterialFee());
        lqw.eq(bo.getTestFee() != null, ClaimAttach::getTestFee, bo.getTestFee());
        lqw.eq(bo.getRepairFee() != null, ClaimAttach::getRepairFee, bo.getRepairFee());
        lqw.eq(bo.getExpressFee() != null, ClaimAttach::getExpressFee, bo.getExpressFee());
        lqw.eq(bo.getHomeVisitFee() != null, ClaimAttach::getHomeVisitFee, bo.getHomeVisitFee());
        lqw.eq(bo.getMileageFee() != null, ClaimAttach::getMileageFee, bo.getMileageFee());
        lqw.eq(bo.getTowingFee() != null, ClaimAttach::getTowingFee, bo.getTowingFee());
        lqw.eq(bo.getOtherFee() != null, ClaimAttach::getOtherFee, bo.getOtherFee());
        lqw.eq(bo.getResidualRecoveryAmount() != null, ClaimAttach::getResidualRecoveryAmount, bo.getResidualRecoveryAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getMode()), ClaimAttach::getMode, bo.getMode());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationTime()), ClaimAttach::getEvaluationTime, bo.getEvaluationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefaultPraise()), ClaimAttach::getIsDefaultPraise, bo.getIsDefaultPraise());
        lqw.eq(StringUtils.isNotBlank(bo.getDataUpdateTime()), ClaimAttach::getDataUpdateTime, bo.getDataUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPerformanceOrderStatusDesc()), ClaimAttach::getPerformanceOrderStatusDesc, bo.getPerformanceOrderStatusDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getReverseWaybillNo()), ClaimAttach::getReverseWaybillNo, bo.getReverseWaybillNo());
        lqw.eq(StringUtils.isNotBlank(bo.getLiabilityDeterminationTime()), ClaimAttach::getLiabilityDeterminationTime, bo.getLiabilityDeterminationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReportLiabilityResult()), ClaimAttach::getReportLiabilityResult, bo.getReportLiabilityResult());
        lqw.eq(StringUtils.isNotBlank(bo.getFinalLiabilityResult()), ClaimAttach::getFinalLiabilityResult, bo.getFinalLiabilityResult());
        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsLiabilityResult()), ClaimAttach::getLogisticsLiabilityResult, bo.getLogisticsLiabilityResult());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceCompanyLiabilityRejectReason()), ClaimAttach::getInsuranceCompanyLiabilityRejectReason, bo.getInsuranceCompanyLiabilityRejectReason());
        lqw.eq(StringUtils.isNotBlank(bo.getReverseInsuranceCompanyCode()), ClaimAttach::getReverseInsuranceCompanyCode, bo.getReverseInsuranceCompanyCode());
        lqw.like(StringUtils.isNotBlank(bo.getReverseInsuranceCompanyName()), ClaimAttach::getReverseInsuranceCompanyName, bo.getReverseInsuranceCompanyName());
        lqw.eq(bo.getClaimAmount() != null, ClaimAttach::getClaimAmount, bo.getClaimAmount());
        lqw.eq(bo.getRecoveredClaimAmount() != null, ClaimAttach::getRecoveredClaimAmount, bo.getRecoveredClaimAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getRecoveredPartyType()), ClaimAttach::getRecoveredPartyType, bo.getRecoveredPartyType());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentTime()), ClaimAttach::getPaymentTime, bo.getPaymentTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentStatus()), ClaimAttach::getPaymentStatus, bo.getPaymentStatus());
        lqw.eq(bo.getCompromiseIncentiveFee() != null, ClaimAttach::getCompromiseIncentiveFee, bo.getCompromiseIncentiveFee());
        lqw.eq(StringUtils.isNotBlank(bo.getIsAccessory()), ClaimAttach::getIsAccessory, bo.getIsAccessory());
        lqw.eq(StringUtils.isNotBlank(bo.getUserStratificationLevel()), ClaimAttach::getUserStratificationLevel, bo.getUserStratificationLevel());
        lqw.like(StringUtils.isNotBlank(bo.getUserStratificationName()), ClaimAttach::getUserStratificationName, bo.getUserStratificationName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserStratificationType()), ClaimAttach::getUserStratificationType, bo.getUserStratificationType());
        lqw.eq(StringUtils.isNotBlank(bo.getUserTagCallTime()), ClaimAttach::getUserTagCallTime, bo.getUserTagCallTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDeliveredReceived()), ClaimAttach::getIsDeliveredReceived, bo.getIsDeliveredReceived());
        lqw.eq(bo.getNewMachineInvoiceRate() != null, ClaimAttach::getNewMachineInvoiceRate, bo.getNewMachineInvoiceRate());
        lqw.eq(StringUtils.isNotBlank(bo.getIsNewMachineInvoiced()), ClaimAttach::getIsNewMachineInvoiced, bo.getIsNewMachineInvoiced());
        lqw.eq(StringUtils.isNotBlank(bo.getIsAfterSalesNewIntegration()), ClaimAttach::getIsAfterSalesNewIntegration, bo.getIsAfterSalesNewIntegration());
        lqw.eq(StringUtils.isNotBlank(bo.getLiabilityCode()), ClaimAttach::getLiabilityCode, bo.getLiabilityCode());
        lqw.like(StringUtils.isNotBlank(bo.getLiabilityName()), ClaimAttach::getLiabilityName, bo.getLiabilityName());
        return lqw;
    }

    /**
     * 新增理赔附属
     *
     * @param bo 理赔附属
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ClaimAttachBo bo) {
        ClaimAttach add = MapstructUtils.convert(bo, ClaimAttach.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改理赔附属
     *
     * @param bo 理赔附属
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ClaimAttachBo bo) {
        ClaimAttach update = MapstructUtils.convert(bo, ClaimAttach.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ClaimAttach entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除理赔附属信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<ClaimAttachVo> queryListByClaimIds(List<String> claimIds) {
        LambdaQueryWrapper<ClaimAttach> lqw = Wrappers.lambdaQuery();
        lqw.in(ClaimAttach::getClaimId, claimIds);
        return baseMapper.selectVoList(lqw);
    }

}
